<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class BatchController extends BaseController {

	/*
	 * 发送手机验证码
	 */
	public function verifynote(){
		$tel 	 = I('post.usertel');
		$fun 	 = new \Org\Util\fun();
		$tmppwd  = substr(mt_rand()."000000",0,6);
		$ssmmess = $fun -> SendSms($tel,$tmppwd);
		session('verftel',null);
		session('verftel',$tmppwd);
		//echo $tmppwd;
	}
	
	/*
	 * 获取session保存的验证码
	 */
	public function getverifynote(){
		$userpass = I('post.userpass');
		if(!empty(session('verftel'))){
			if($userpass==session('verftel')){
				echo 1; //通过
			}else{
				echo 0; //不正确
			}
		}else{
			echo 2;//验证码过期
		}
	}
	
	/*
	 * 真一物一码
	 * 手机验证通过保存
	 */
	public function savetel(){
		$tel 		= I('post.usertel');
		$randma 	= I('post.randma');
		$useropenid = I('post.useropenid');
		$telres 	= D(C('TABLE_TELINFO'));
		$num 		= $telres->where(array('tel'=>$tel))->count();
		if($num < 1){
			$data['ma_randinfo'] 	= $randma;
			$data['tel'] 			= $tel;
			$data['useropenid'] 	= $useropenid;
			$data['regdate'] 		= date("Y-m-d H:i:s", time());
			$res = $telres->add($data);
			if($res){
				echo 1;//保存成功
			}else{
				echo 2;//保存失败
			}
		}else{
			echo 3;//存在此号码
		}
	}

	/*
	 * 固定码
	 * 手机验证通过保存
	 */
	public function savetelfalse(){
		$tel 		= I('post.usertel');
		$rootid 	= I('post.rootid');
		$useropenid = I('post.useropenid');
		$telres 	= D(C('TABLE_TELINFO'));
		$res = $telres->where(array('ma_randinfo'=>$rootid,'useropenid'=>$useropenid,'tel'=>$tel))->find();
		if($res){
			//如果存在openid和手机号码则显示领取次数
			if($res['hbnum']!=1){
				echo 0;
			}else{
				echo 1;
			}
		}else{
			//保存openid和手机号码信息
			$data['ma_randinfo'] 	= $rootid;
			$data['tel'] 			= $tel;
			$data['useropenid'] 	= $useropenid;
			$data['regdate'] 		= date("Y-m-d H:i:s", time());
			$res = $telres->add($data);
			if($res){
				echo 'ok';//保存成功
			}else{
				echo -1;//保存失败
			}
		}
	}
	
	public function savetel1(){
		$telres = D(C('TABLE_TELINFO'));
		$tel 	= I('post.usertel');
		$num 	= $telres->where(array('tel'=>$tel))->find();
		if($num){
			echo $num['hbnum'];
		}else{
			echo 'no';//不存在
		}
	}

	//判断是否手机号是否被注册过
	public function isreginfo(){
		$telres = D(C('TABLE_TELINFO'));
		$tel 	= I('post.usertel');
		$num 	= $telres->where(array('tel'=>$tel))->count();
		if($num > 0){
			echo 1; //存在
		}else{
			echo 0; //不存在
		}
	}


    /*
     * 真一物一码
     * 抢红包成功则设置该码的状态
     */
    public function setmastate(){
    	$randma 		= I('post.randma');
    	$total_amount 	= I('post.total_amount');
    	$openid 		= I('post.openid');
    	$mch_billno 	= I('post.mch_billno');
    	$status 		= I('post.status');
    	if($status==1){		// 如果红包发放成功，则返回1
    		$rand = D(C('TABLE_RANDNUMBER_HB'));
    		$num  = $rand->where(array('ma_randnumber'=>$randma))->count();
    		if($num > 0){	// 开始判断是否已经被领取一次红包+2
    			$this->savehbdata($randma, $total_amount, $openid, $mch_billno, 2);
    		}else{			// 开始判断是否已经被领取一次红包+1
    			$this->savehbdata($randma, $total_amount, $openid, $mch_billno, 1);
    		}
    	}else{
    		echo 2; //参数错误
    	}
    }
    
    /*
     * 真一物一码
     * 检测红包码是否有效
     */
    public function checkhbma(){
    	$randma = I('post.randma');
    	$rand   = D(C('TABLE_RANDNUMBER'));
    	$num    = $rand->where(array('ma_randnumber' => $randma))->count();
    	if($num>0){
    		echo 1;//存在
    	}else{
    		echo 0;//不存在
    	}
    }

	/*
     * 固定码
     * 检测红包码是否有效
     */
	public function checkhbmafasle(){
		$rootid = I('post.rootid');
		$type   = D(C('TABLE_TYPE'));
		$num    = $type->where(array('id' => $rootid,'isvote' => 1))->count();
		if($num>0){
			echo 1;//存在
		}else{
			echo 0;//不存在
		}
	}
	public function setmastatefalse(){
		$rootid = I('post.rootid');
		$openid = I('post.openid');
		$status = I('post.status');
		if($status==1){
			$telres = D(C('TABLE_TELINFO'));
			$data['hbnum'] = 1;
			$res = $telres->where(array('ma_randinfo' => $rootid,'useropenid' => $openid))->save($data);
			if($res){
				echo 1;//更新成功
			}else{
				echo 0;//更新失败
			}
		}
	}

	public function islogin($getURL){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:".C('LOGINADDRES').$getURL);
			exit;
		}
	}


	protected function savehbdata($randma, $total_amount, $openid, $mch_billno, $cishutoal){
    	$rand = D(C('TABLE_RANDNUMBER_HB'));
    	$data['ma_randnumber']  	= $randma;
    	$data['ma_money']       	= $total_amount;
    	$data['ma_money_state'] 	= 1;
    	$data['ma_user_openid'] 	= $openid;
    	$data['ma_mch_billno']  	= $mch_billno;
    	$data['ma_randnumber_date'] = date("Y-m-d H:i:s", time());
    	$res = $rand->add($data);
    	if($res){
    		$this->setmahbcishu($randma,$cishutoal);
    		echo 1; //成功
    	}else{
    		echo 0; //失败
    	}
    }

	/*
     * 固定码
     * 获取红包领取次数
     */
	public function getcishufalse(){
		$rootid   = I('post.rootid');
		$userinfo = I('post.userinfo');
		$telinfo  = D(C('TABLE_TELINFO'));
		$res      = $telinfo->where(array('ma_randinfo' => $rootid,'useropenid' => $userinfo))->find();
		if($res){
			if($res['hbnum']!=1){
				echo 0; //未领取
			}else{
				echo 1; //已领取
			}
		}else{
			echo -1; //没有此信息
		}
	}
    
    
    /*
     * 领取完跳转的页面
     */
    public function getpackets(){
    	$money  = number_format(I('get.money')*0.01, 2);
    	$randma = I('get.getma');
    	$rand   = D(C('TABLE_RANDNUMBER'));
    	$res    = $rand->where(array('ma_randnumber' => $randma))->find();
    	if($res){
    		$this->assign('mainfo',$res);
    		$this->assign('money',$money); //金额
    		$this->display('Index/getpackets');
    	}else{
    		echo "<script>alert('此码不存在！');</script>";
    	}
    }


	//如果是红包码则插入用户的openid信息
	public function saveuseropenid(){
		$userinfo 	= $_COOKIE['user_userinfo'];
		$randcode 	= I('post.randcode');
		$randma 	= D(C('TABLE_RANDNUMBER'));
		$data['iswhoget'] = $userinfo;
		$res = $randma->where(array('ma_randnumber' => $randcode))->save($data);
		if($res){
			$res = array('ret'=>1);
			$this->inserttjinfo($randcode);
		}else{
			$res = array('ret'=>0);
		}
		echo json_encode($res);
	}


	//添加访问者信息
	protected function inserttjinfo($maid){
		$userinfo 	= $_COOKIE['user_userinfo'];
		$tjinfo 	= iptoarea();
		$ip 		= $tjinfo['ip'];
		$statistics = D(C('TABLE_STATISTICS'));
		$count 		= $statistics->where(array('useropenid' => $userinfo,'ip' => $ip))->count();
		if($count==0){
			if(!empty($ip)){
				$data['useropenid'] = $userinfo;
				$data['maid'] 		= $maid;
				$data['country'] 	= $tjinfo['country'];
				$data['area'] 		= $tjinfo['area'];
				$data['region'] 	= $tjinfo['region'];
				$data['city'] 		= $tjinfo['city'];
				$data['county'] 	= $tjinfo['county'];
				$data['isp'] 		= $tjinfo['isp'];
				$data['ip'] 		= $ip;
				$data['visitdate'] 	= date("Y-m-d H:i:s", time());
				$res = $statistics->add($data);
				return $res ? true : false;
			}
		}
	}
	
	/* 话费充值业务处理 */
	public function topupbusiness(){
		if(islogin(getURL())){
			$randcode 	= session('randcode');
			$tel 		= I('post.usertel');

			$randnumber 		= W('Public/get_randcode_info', array('randcode' => $randcode));
			// 获取批次
			$batchnumber 		= $randnumber['batchnumber'];
			$award 				= W('Public/cx_award', array('ma_type' => $randnumber['ma_type']));
			//检测手机号码是否能充值
			$telcheck_result 	= W('Public/telcheck', array('phoneno' => $tel, 'cardnum' => $award['awardteltopup']));
			if($telcheck_result['error_code'] == 0){
				//通过检测，直接充值
				if(W('Public/onlineorder', array('phoneno' => $tel, 'cardnum' => $award['awardteltopup'], 'randnumber' => $randnumber))){
					$result = array('error_code' => 0, 'message' => '正在充值，请稍后查看余额变动');
				}
			}else{
				$result = array('error_code' => 400, 'message' => $telcheck_result['reason']);
			}
			echo json_encode($result);
		}
	}
	
	/* 流量充值业务处理 */
	public function flowbusiness(){
		if(islogin(getURL())){
			$randcode 	= session('randcode');
			$tel 		= I('post.usertel');
			
			$randnumber 		= W('Public/get_randcode_info', array('randcode' => $randcode));
			$award 				= W('Public/cx_award', array('ma_type' => $randnumber['ma_type']));
			$award_flow 		= $award['awardkind'] == 4 ? W('Public/cx_flow_rule', array('award_id' => $award['id'], 'awardkind' => $award['awardkind'])) : null;
			//检测手机号码是否能充值
			$telcheck_result 	= W('Public/telcheck_flow', array('phoneno' => $tel));
			if($telcheck_result['error_code'] == 210501){
				$result = array('error_code' => $telcheck_result['error_code'], 'message' => $telcheck_result['reason']);
			}else{
				$telcheck_flow  = $telcheck_result['result'][0]['companytype'];
				$tel_flow_list  = $telcheck_result['result'][0]['flows'];
				//根据不同运营商的ID号获取不同奖励规则的流量套餐ID
				if(!empty($award_flow)){
					foreach ($award_flow as $v){
						if($v['operator_id'] == $telcheck_flow){
							$flow_rule = $v['flow_id'];
						}
					}
				}
				$flow_id_array = explode(',',$flow_rule);
				//根据多个流量套餐ID随机取出一个流量套餐ID号
				$flow_id_val_only  = $flow_id_array[ array_rand($flow_id_array, 1) ];
				//提交流量充值
				$res    = W('Public/execute_recharge', array('phoneno' => $tel, 'pid' => $flow_id_val_only, 'randnumber' => $randnumber, 'setmeal' => $telcheck_result));
				if($res) $result = array('error_code' => 0, 'message' => '充值成功，将于24小时内生效');
			}
			echo json_encode($result);
		}
	}


}